package ${packageName}.dao.impl;

import com.jspx.sober.Criteria;
import com.jspx.sober.jdbc.JdbcOperations;
import com.jspx.sober.ssql.SSqlExpression;
import com.jspx.utils.StringUtil;
import com.jspx.utils.ArrayUtil;
import com.jspx.sober.criteria.expression.Expression;
import com.jspx.sober.criteria.projection.Projections;
import java.text.ParseException;
import java.util.*;
import ${packageName}.dao.*;
import ${packageName}.table.*;


/**
 * Created by jspx.net maker
 * User: chenYuan
 * DateTime: ${date.string('yyyy-MM-dd HH:mm')} 
 * jspx.jquestion.dao.impl.SurveyDAOImpl
 */
public class ${className}DAOImpl extends JdbcOperations implements ${className}DAO {


    public ${className}DAOImpl() {

    }
    /**
    * 排序
    *
    * @param ids      id array
    * @param sortType 排序类型
    * @return boolean 是否成功
    */
    public boolean updateSortType(Long[] ids, int sortType)
    {
        if (null == ids) return true;
        try
        {
            for (Long mId : ids)
            {
                if (mId<1) continue;
                ${className} ${className.firstLowerCase()} = (${className}) get(${className}.class, mId, false);
                if (${className.firstLowerCase()} != null)
                {
                    ${className.firstLowerCase()}.setSortType(sortType);
                    super.update(${className.firstLowerCase()}, new String[]{"sortType"});
                }
            }
        } catch (Exception e)
        {
            log.error(ArrayUtil.toString(ids,StringUtil.COMMAS),e);
            return false;
        }
        return true;
    }

    /**
    * 排序时间
    *
    * @param ids id array
    * @return boolean  是否成功
    */
    public boolean updateSortDate(Long[] ids)
    {
        if (null == ids) return true;
        try
        {
            for (Long mId : ids)
            {
                if (mId<1) continue;
                ${className} ${className.firstLowerCase()} = (${className}) get(${className}.class, mId, false);
                if (${className.firstLowerCase()} != null)
                {
                    ${className.firstLowerCase()}.setSortDate(new Date());
                    super.update( ${className.firstLowerCase()}, new String[]{"sortDate"});
                }
            }
        } catch (Exception e)
        {
            log.error(ArrayUtil.toString(ids,StringUtil.COMMAS),e);
            return false;
        }
        return true;
    }
    /**
     * @param find 查询
     * @param term 条件
     * @param uid 用户id
     * @param sort 排序方式
     * @param page 页数
     * @param count 数量
     * @return    得到列表
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public List<${className}> getList(String[] fields,String[] find, String term, long uid, String sort, int page, int count) throws Exception {
        Criteria criteria = createCriteria(${className}.class);
        if (!ArrayUtil.isEmpty(find)&& !ArrayUtil.isEmpty(fields)) {
            criteria = criteria.add(Expression.find(fields,find));
        }
        if (uid>0) {
            criteria = criteria.add(Expression.eq("putUid", uid));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        criteria = SSqlExpression.getSortOrder(criteria, sort);
        return (List<${className}>) criteria.setCurrentPage(page).setTotalCount(count).list(false);
    }

    /**
     * @param find 查询
     * @param term 条件
     * @param uid  用户id
     * @return 返回数据条数
     * @throws Exception 异常
     */
    public int getCount(String[] fields,String[] find, String term, long uid) throws Exception {
        Criteria criteria = createCriteria(${className}.class);
        if (!ArrayUtil.isEmpty(find)&& !ArrayUtil.isEmpty(fields)) {
            criteria = criteria.add(Expression.find(fields,find));
        }
        if (uid>0) {
            criteria = criteria.add(Expression.eq("putUid", uid));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        return criteria.setProjection(Projections.rowCount()).intUniqueResult();
    }
}